const { DataTypes } = require('sequelize');
const crypto = require('crypto');

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // 创建用户表
    await queryInterface.createTable('user', {
      id: {
        type: DataTypes.BIGINT,
        primaryKey: true,
        autoIncrement: true,
        comment: '用户ID'
      },
      username: {
        type: DataTypes.STRING(32),
        allowNull: false,
        unique: true,
        comment: '用户名'
      },
      password: {
        type: DataTypes.STRING(32),
        allowNull: false,
        comment: '密码'
      },
      name: {
        type: DataTypes.STRING(32),
        allowNull: true,
        comment: '姓名'
      },
      phone: {
        type: DataTypes.STRING(11),
        allowNull: true,
        comment: '手机号'
      },
      status: {
        type: DataTypes.TINYINT,
        allowNull: false,
        defaultValue: 1,
        comment: '状态：0-禁用 1-正常'
      },
      last_login_time: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: '最后登录时间'
      },
      create_time: {
        type: DataTypes.DATE,
        allowNull: false,
        comment: '创建时间'
      },
      update_time: {
        type: DataTypes.DATE,
        allowNull: false,
        comment: '更新时间'
      }
    });

    // 创建管理员表
    await queryInterface.createTable('admin', {
      id: {
        type: DataTypes.BIGINT,
        primaryKey: true,
        autoIncrement: true,
        comment: '管理员ID'
      },
      username: {
        type: DataTypes.STRING(32),
        allowNull: false,
        unique: true,
        comment: '用户名'
      },
      password: {
        type: DataTypes.STRING(32),
        allowNull: false,
        comment: '密码'
      },
      name: {
        type: DataTypes.STRING(32),
        allowNull: true,
        comment: '姓名'
      },
      phone: {
        type: DataTypes.STRING(11),
        allowNull: true,
        comment: '手机号'
      },
      status: {
        type: DataTypes.TINYINT,
        allowNull: false,
        defaultValue: 1,
        comment: '状态：0-禁用 1-正常'
      },
      last_login_time: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: '最后登录时间'
      },
      create_time: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW,
        comment: '创建时间'
      },
      update_time: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW,
        comment: '更新时间'
      }
    });

    // 创建默认管理员账号
    const password = '123456';
    const md5 = crypto.createHash('md5');
    const hash = md5.update(password).digest('hex');
    await queryInterface.bulkInsert('admin', [{
      username: 'admin',
      password: hash,
      name: '管理员',
      status: 1,
      create_time: new Date(),
      update_time: new Date()
    }]);

    // 创建分类表
    await queryInterface.createTable('category', {
      id: {
        type: DataTypes.BIGINT,
        primaryKey: true,
        autoIncrement: true,
        comment: '分类ID'
      },
      name: {
        type: DataTypes.STRING(32),
        allowNull: false,
        comment: '分类名称'
      },
      icon: {
        type: DataTypes.STRING(256),
        allowNull: true,
        comment: '分类图标'
      },
      description: {
        type: DataTypes.STRING(512),
        allowNull: true,
        comment: '分类描述'
      },
      parent_id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        defaultValue: 0,
        comment: '父分类ID'
      },
      level: {
        type: DataTypes.TINYINT,
        allowNull: false,
        defaultValue: 1,
        comment: '层级：1-一级 2-二级'
      },
      sort_order: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '排序'
      },
      status: {
        type: DataTypes.TINYINT,
        allowNull: false,
        defaultValue: 1,
        comment: '状态：0-禁用 1-正常'
      },
      create_time: {
        type: DataTypes.DATE,
        allowNull: false,
        comment: '创建时间'
      },
      update_time: {
        type: DataTypes.DATE,
        allowNull: false,
        comment: '更新时间'
      }
    });

    // 添加测试分类数据
    await queryInterface.bulkInsert('category', [
      {
        name: '手机数码',
        icon: '/uploads/categories/digital.png',
        level: 1,
        sort_order: 1,
        status: 1,
        create_time: new Date(),
        update_time: new Date()
      },
      {
        name: '服装鞋包',
        icon: '/uploads/categories/clothing.png',
        level: 1,
        sort_order: 2,
        status: 1,
        create_time: new Date(),
        update_time: new Date()
      }
    ]);
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('user');
    await queryInterface.dropTable('admin');
    await queryInterface.dropTable('category');
  }
}; 